# coding=utf-8
import os
import cv2
import imageio
import argparse
from PIL import Image

parser = argparse.ArgumentParser(description='img to vedio or gif')
parser.add_argument('--config', default='C:/Users/DZF/Desktop/anti block result/deer-3/anti_block/result/',
                    type=str, help='config file')
args = parser.parse_args()

# fps: 帧率
def make_video(video_path, fps):
    print("video_path:", video_path)
    # fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")

    for image_name in os.listdir(root_path):
        img_path = os.path.join(root_path, image_name)
        break
    im = Image.open(img_path)
    print("im.size:", im.size)
    vw = cv2.VideoWriter(video_path, fourcc, fps, im.size)
    # vw = cv2.VideoWriter(path, 0x00000021, fps, im.size)
    for image_name in os.listdir(root_path):
        frame = cv2.imread(root_path + '/' + image_name)
        vw.write(frame)
    print('Save video successfully!')

def make_gif(gif_path, duration):
    print("gif_path:",gif_path)
    frames = []
    for image_name in os.listdir(root_path):
        frames.append(imageio.imread(root_path + '/' + image_name))
    imageio.mimsave(gif_path, frames, 'GIF', duration=duration)
    print('Save gif successfully!')
    return


if __name__ == '__main__':
    root_path = args.config
    video_path = os.path.join(root_path, 'test.mp4')
    gif_path = os.path.join(root_path, 'test.gif')
    vedio_fps = 10
    gif_duration = 0.05

    # make_video(video_path, vedio_fps)
    make_gif(gif_path, gif_duration)


